import BaseEnumValue from '@/core/utils/BaseEnumValue'
import lodash from 'lodash'

export default class BaseEnum<T> {
	private readonly propertyNames: string[]

	constructor(propertyNames: string[]) {
		this.propertyNames = propertyNames
	}

	resolve = <E>(value: T): E | undefined => {
		const self: any = this
		const propertyName = this.propertyNames.find(propertyName => {
			const baseEnum: BaseEnumValue<T> = self[propertyName]
			return baseEnum.getCode() === value
		})
		if (lodash.isNil(propertyName)) {
			return undefined
		}
		return self[propertyName]
	}

	getAll = (): BaseEnumValue<T>[] => {
		const self: any = this
		return this.propertyNames
			.map(propertyName => {
				return self[propertyName]
			})
			.filter(p => !lodash.isNil(p))
	}
}
